﻿#region Copyright RenGuiYou. All rights reserved.
//=====================================================
// NeatlyFrameWork
// Author:      RenGuiyou
// Feedback: 	mailto:750539605@qq.com
//=====================================================
#endregion
using System.Text;
using Neatly;

public class HashReader : Singleton<HashReader>
{
    private byte[] data;
    private int offset;
    private readonly byte[] md5Bytes = new byte[16];

    public void InitBytes(byte[] bytes)
    {
        data = bytes;
        offset = 0;
    }

    public byte ReadByte()
    {
        byte result = data[offset];
        offset++;
        return result;
    }

    public int ReadInt()
    {
        int result = data[offset] << 24 | data[offset + 1] << 16 | data[offset + 2] << 8 | data[offset + 3];
        offset += 4;
        return result;
    }

    public string ReadMD5String()
    {
        for (var i = 0; i < md5Bytes.Length; i++)
        {
            md5Bytes[i] = ReadByte();
        }
        return HashHelper.ToHexString(md5Bytes);
    }

    public string ReadString()
    {
        int num = ReadByte();
        string str = Encoding.UTF8.GetString(data, offset, num);
        offset += num;
        return str;
    }

    public void ReadBytes(ref byte[] array)
    {
        int count = ReadByte();
        for (int i = 0; i < count; i++)
        {
            var b = ReadByte();
            array[i] = b;
        }
    }
}